<template>
  <div class="swiper-container">
    <div class="swiper-wrapper">
      <div class="swiper-slide" v-for="(item, index) in list" :key="index">
        <img :src="item.imgUrl" alt="" />
      </div>
    </div>
    <!-- 如果需要分页器 -->
    <div class="swiper-pagination"></div>

    <!-- 如果需要导航按钮 -->
    <div class="swiper-button-prev"></div>
    <div class="swiper-button-next"></div>

    <!-- 如果需要滚动条 -->
    <!-- <div class="swiper-scrollbar"></div> -->
  </div>
</template>

<script>
// 引入swiper的js
import Swiper from 'swiper/swiper-bundle.js'
import 'swiper/swiper-bundle.min.css'
export default {
  props: ['list'],
  watch: {
    list: {
      immediate: true,
      handler() {
        this.$nextTick(() => {
          new Swiper('.swiper-container', {
            direction: 'horizontal', // 垂直切换选项
            loop: true, // 循环模式选项

            // 如果需要分页器
            pagination: {
              el: '.swiper-pagination',
              clickable: true,
              type: 'bullets'
            },

            // 如果需要前进后退按钮
            navigation: {
              nextEl: '.swiper-button-next',
              prevEl: '.swiper-button-prev'
            },

            // 如果需要滚动条
            // scrollbar: {
            //   el: '.swiper-scrollbar'
            // }

            // 自动切换
            autoplay: {
              delay: 2000,
              disableOnInteraction: false
            }
          })
        })
      }
    }
  }
}
</script>

<style lang="less" scoped>
.swiper-container {
  width: 100%;
  height: 100%;
  img {
    width: 100%;
    height: 100%;
  }
  .swiper-button-prev {
    left: 0;
  }
  .swiper-button-next {
    right: 0;
  }
  .swiper-button-prev,
  .swiper-button-next {
    width: 40px;
    height: 60px;
    display: none;
    transform: translateY(-50%);
  }

  &:hover .swiper-button-prev,
  &:hover .swiper-button-next {
    display: flex;
  }
}
</style>
